package org.example.smartbuy.controller;

import org.example.smartbuy.Result;
import org.example.smartbuy.entity.Orders;
import org.example.smartbuy.service.IOrdersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/orders")
@Tag(name = "Orders Controller", description = "Operations related to orders")
public class OrdersController {

    @Autowired
    private IOrdersService ordersService;

    /**
     * 获取所有订单
     * @return List<Orders>
     */
    @GetMapping("/list")
    @Operation(summary = "Get all orders records", description = "Fetch a list of all orders records",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result listAllOrdersById(int userId) {
        return Result.success(ordersService.listById(userId));
    }

    /**
     * 根据ID查询单个订单
     * @param id 订单ID
     * @return Orders
     */
    @GetMapping("/{id}")
    @Operation(summary = "Get an order record by ID", description = "Fetch order by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result getOrderById(@PathVariable int id) {
        return Result.success(ordersService.getById(id));
    }

    /**
     * 添加订单
     * @param orders 订单对象
     * @return Orders
     */
    @PostMapping("/add")
    @Operation(summary = "Add a new order record", description = "Create a new order record",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result addOrder(@RequestBody Orders orders) {
        return Result.success(ordersService.add(orders));
    }

    /**
     * 更新订单信息 主要是订单状态
     * @param orders 更新的订单信息
     * @return Orders
     */
    @PutMapping("/update")
    @Operation(summary = "Update an order record", description = "Update an existing order record",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result updateOrderStatus(@RequestBody Orders orders) {
        return Result.success(ordersService.update(orders));
    }

    /**
     * 删除订单
     * @param id 订单ID
     */
    @DeleteMapping("/delete/{id}")
    @Operation(summary = "Delete an order record", description = "Delete an order record by its ID",
            responses = {
                    @ApiResponse(responseCode = "200", description = "操作成功"),
                    @ApiResponse(responseCode = "400", description = "操作失败")
            })
    public Result deleteOrder(@PathVariable int id) {
        ordersService.delete(id);
        return Result.success();
    }
}
